package com.hrms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hrms.pojo.Department;
import com.hrms.pojo.Employee;
import com.hrms.service.DepartmentService;

@Controller
@RequestMapping("/depart")
public class DepartmentController {

	@Resource
	private DepartmentService departmentService;
	
	//部门模块跳转
	@RequestMapping("/dep_f")
	public String depart() {
		return "depart/depart_f";
	}

	// 根据id删除部门
	@RequestMapping("/{dtId}/remove")
	public String remove(@PathVariable("dtId") Integer dtId) {
		if (dtId == null) {
			return "forward:/error/error";
		}
		Department dep = departmentService.getDepartById(dtId);
		if (dep == null) {
			return "forward:/error/error";
		}
		departmentService.removeDepart(dtId);
		return "depart/rem_succ";
	}

	// 查看所有部门,显示信息
	@RequestMapping("/list_dep")
	public String list(Model model) {
		List<Department> departList = departmentService.getDepartALL();
		model.addAttribute("departList", departList);
		return "depart/list_dep";
	}

	// 修改部门跳转
	@RequestMapping("/alter")
	public String alter() {
		return "depart/alter_dep";
	}

	// 修改部门信息
	@RequestMapping(value = "/{dtId}/alter_dep", method = RequestMethod.POST)
	public String alterDepart(HttpServletRequest req, @PathVariable("dtId") Integer dtId) {
		// 验证id是否为null
		if (dtId == null) {
			return "forward:/error/error";
		}
		// try {
		// dtId = Integer.parseInt(dtIdStr);
		// } catch (NumberFormatException e1) {
		// return "forward:/error/error";
		// }
		// 验证该id的部门是否存在
		Department dep = departmentService.getDepartById(dtId);
		if (dep == null) {
			return "forward:/error/error";
		}
		String dtName = req.getParameter("dtName");
		Department depByName = departmentService.getByName(dtName);
		// 验证新名称是否存在
		if (depByName != null) {
			return "depart/alter_fail";
		}
		String dtCreatetime = req.getParameter("dtCreatetime");
		String dtBz = req.getParameter("dtBz");
		// if("".equals(dtName) && "".equals(dtCreatetime) && "".equals(dtBz)) {
		// return "depart/alter_dep";
		// }
		if ("".equals(dtCreatetime))
			dtCreatetime = dep.getDtCreatetime();
		if ("".equals(dtBz))
			dtBz = dep.getDtBz();
		dep.setDtName(dtName);
		dep.setDtCreatetime(dtCreatetime);
		dep.setDtBz(dtBz);
		try {
			departmentService.alterDepart(dep);
		} catch (Exception e) {
			return "forward:/error/error";
		}
		return "depart/alter_succ";
	}

	// 添加部门跳转
	@RequestMapping("/add")
	public String add() {
		return "depart/add_dep";
	}

	// 添加部门信息
	@RequestMapping(value = "/add_depart", method = RequestMethod.POST)
	public String addDepart(HttpServletRequest req) {
		String dtName = req.getParameter("dtName");
		String dtCreatetime = req.getParameter("dtCreatetime");
		String dtBz = req.getParameter("dtBz");
		Department dep = new Department();
		dep.setDtName(dtName);
		dep.setDtCreatetime(dtCreatetime);
		dep.setDtBz(dtBz);
		try {
			departmentService.addDepart(dep);
		} catch (Exception e) {
			return "forward:/error/error";
		}
		return "depart/add_succ";
	}

	// 根据部门查找跳转
	@RequestMapping("/seldep")
	public String selDep(Model model) {
		List<Department> departList = departmentService.getDepartALL();
		model.addAttribute("departList", departList);
		return "depart/seldep";
	}

	// 根据部门id查找员工
	@RequestMapping(value = "/withemp")
	public String getByIdWithEmp(HttpServletRequest req, Model model) {
		int dtId = Integer.parseInt(req.getParameter("dtId"));
		Department dep = departmentService.getDepartById(dtId);
		if (dep == null) {
			return "forward:/error/error";
		}
		String dtName = dep.getDtName();
		// req.getSession().setAttribute("dtName", dtName);
		model.addAttribute("dtName", dtName);
		List<Employee> empList = departmentService.getByIdWithEmp(dtId);
		model.addAttribute("empList", empList);
		return "depart/dep_emps";
	}
}
